from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from sqlalchemy.orm import sessionmaker
from app.core.config import settings

# 创建异步数据库引擎
async_engine = create_async_engine(
    settings.DATABASE_URL, 
    echo=False,
    pool_pre_ping=True,   # 防止使用断开的连接
    pool_recycle=1800,    # 链接池自动回收（30分钟）
    pool_timeout=30,      # 链接池超时时间（30秒）
    future=True,
)

# 创建异步数据库会话工厂
async_db_session = sessionmaker(
    async_engine, class_=AsyncSession, expire_on_commit=False
)

async def get_db():
    async with async_db_session() as session:
        yield session


